/*
 * @lc app=leetcode.cn id=416 lang=javascript
 *
 * [416] 分割等和子集
 */

// @lc code=start
/**
 * @param {number[]} nums
 * @return {boolean}
 */
var canPartition = function (nums) {
  const sum = nums.reduce((pre, cur) => pre + cur, 0);
  let size = sum / 2;
  const len = nums.length;
  let dp = new Array(size + 1).fill(0);
  for (let i = 1; i <= len; i++) {
    for (let j = size; j >= nums[i - 1]; j--) {
      dp[j] = Math.max(dp[j], nums[i - 1] + dp[j - nums[i - 1]]);
    }
  }

  return dp[size] === size;
};

canPartition([1, 5, 11, 5]);
// @lc code=end
